//
//  FYAlertTooler.m
//  FYAgent
//
//  Created by 于龙 on 15/11/20.
//  Copyright © 2015年 Foryou. All rights reserved.
//

#import "FYAlertTooler.h"
#import "AppDelegate.h"

@interface FYAlertTooler()<FYMessageViewDelegate>
@property (nonatomic, strong) FYMessageView *msgView;
@end

@implementation FYAlertTooler
static FYAlertTooler *_shareInstance = nil;

+ (FYAlertTooler *)sharedInstance {
    
    if (!_shareInstance) {
        
        _shareInstance = [[FYAlertTooler alloc] init];
    }
    return _shareInstance;
}

- (void)showAlertTitle:(NSString *)title
               message:(NSString *)msg
             btnTitles:(id)titles
            btnClicked:(void (^)(NSInteger index))click{
    
    self.block = click;
    [self.msgView setAlertTitle:title warningShow:NO message:msg btnTitles:titles];
    AppDelegate *appDelegate = APPDELEGATE;
    [appDelegate.window addSubview:self.msgView];
    [self showAnimation];
}

- (void)showAlertTitle:(NSString *)title
          attributeMsg:(NSAttributedString *)msg
             btnTitles:(id)titles
            btnClicked:(void (^)(NSInteger))click {
    
    self.block = click;
    [self.msgView setAlertTitle:title attributeMsg:msg btnTitles:titles];
    AppDelegate *appDelegate = APPDELEGATE;
    [appDelegate.window addSubview:self.msgView];
    [self showAnimation];
}

- (void)showAlertWithWarningBtnTitle:(NSString *)title
                             message:(NSString *)msg
                           btnTitles:(id)titles
                          btnClicked:(void (^)(NSInteger))click warnBtnClicked:(void (^)(BOOL))warnclick {
    
    self.block = click;
    self.wblock = warnclick;
    [self.msgView setAlertTitle:title warningShow:YES message:msg btnTitles:titles];
    AppDelegate *appDelegate = APPDELEGATE;
    [appDelegate.window addSubview:self.msgView];
    [self showAnimation];
}
- (FYMessageView *)msgView {
    
    if (!_msgView) {
     
        _msgView =  [[NSBundle mainBundle] loadNibNamed:@"FYMessageView" owner:nil options:nil].firstObject;
        AppDelegate *appDelegate = APPDELEGATE;
        _msgView.frame = appDelegate.window.bounds;
        _msgView.delegate = self;
    }
    return _msgView;
}

- (void)showAnimation {
    
    //显示动画
    self.msgView.whiteView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);
    self.msgView.whiteView.alpha = 0;
    [UIView animateWithDuration:0.3f animations:^{
        self.msgView.whiteView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        self.msgView.whiteView.alpha = 1.0;
    }completion:^(BOOL finished) {
        
    }];
}

- (void)dismissAnimation:(void (^)(BOOL success))complete{
    
     //消失动画
    [UIView animateWithDuration:0.3f animations:^{
        self.msgView.whiteView.transform = CGAffineTransformMakeScale(0.01f, 0.01f);
        self.msgView.whiteView.alpha = 0;
    }completion:^(BOOL finished) {
        
        [self.msgView removeFromSuperview];
        self.msgView = nil;
        complete(YES);
    }];
}

#pragma mark - FYMessageView Delegate
- (void)alertView:(FYMessageView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self dismissAnimation:^(BOOL success) {
        
        self.block(buttonIndex);

    }];
}

- (void)alertViewWarnBtnClicked:(FYMessageView *)alertView {
    [self dismissAnimation:^(BOOL success) {
        
        self.wblock(YES);
    }];
}

@end
